<?php
class Users_model extends CI_Model {

    public function __construct()
    {
        $this->load->database();
    }

    public function get_users($slug = FALSE)
    {
        if ($slug === FALSE)
        {
            $query = $this->db->get('users');
            return $query->result_array();
        }
        $query = $this->db->get_where('users', array('slug' => $slug));
        return $query->row_array();
    }

    public function register($slug = FALSE)
    {
      $this->load->helper('url');

      $slug = url_title($this->input->post('username'), 'dash', TRUE);

      $data = array(
          'username' => $this->input->post('username'),
          'password' => $this->input->post('password'),
          'phone' => $this->input->post('phone'),
          'share_code' => $this->input->post('share_code'),
          'slug' => $slug,
          'text' => '备注'
      );

      return $this->db->insert('users', $data);
    }

    public function resultData($data = null, $status = 200, $msg = 'success') {
        return $message = array (
            'status'=>$status,
            'msg'=>$msg,
            'data'=>$data
        );
    }

    public function get_list($slug = NULL)
    {
        if ($slug != NULL) {
            $query = $this->db->get_where($slug);
            $query = $query->result_array();
            return $query;
        }
        $query['list_types'] = $this->db->get_where('list_types');
        $query['list_citys'] = $this->db->get_where('list_citys');
        $query['list_money'] = $this->db->get_where('list_money');
        $query['list_assets'] = $this->db->get_where('list_assets');
        foreach ($query as $key => $value) {
            $query[$key] = $query[$key]->result_array();
        }
        return $query;
    }

    public function set_list($list)
    {
        $this->load->helper('url');

        $slug = url_title($this->input->post('title'), 'dash', TRUE);

        $data = array(
            'title' => $this->input->post('title'),
            'name' => $this->input->post('name'),
            'slug' => $slug
        );

        return $this->db->insert($list, $data);
    }

    public function getNums()
    {
        $query = $this->db->get_where('nums');
        return $query->row_array();
    }

    public function getList($slug = NULL)
    {
        if ($slug == NULL) {
            return 500;
        }
        $query = $this->db->get_where($slug);
        return $query->result_array();
    }

    public function setList($list,$data)
    {
        $this->load->helper('url');

        $slug = url_title($this->input->post('title'), 'dash', TRUE);

        return $this->db->insert($list, $data);
    }
    public function getItem($id = NULL,$slug = NULL) {
        if ($slug == NULL || $id == NULL) {
            return 500;
        }
        $query = $this->db->get_where($slug, array('id' => $id));
        return $query->row_array();
    }
    public function deleteItem($id = NULL,$slug = NULL) {
        if ($slug == NULL || $id == NULL) {
            return 500;
        }
        $this->db->where('id',$id);
        $this->db->delete($slug);
        return 200;
    }

    public function updateItem($id = NULL,$slug = NULL,$data = NULL) {
        if ($slug == NULL || $id == NULL) {
            return 500;
        }
        $this->db->where('id',$id);
        $this->db->update($slug,$data);
        return 200;
    }
}
